﻿#include "FrameworkDataCenter.h"
#include "InjectFramework.h"
#include <Windows.h>
#include "../interface/YunDunDefenderDlg.h"
void PathQueueSet::print_byte_data(std::wstring& data, size_t length)
{
    if (data.length() != length && !m_fkptr)
    {
        return;
    }
    if (data == L"end") //文件扫描完成，可以通知工作线程结束
    {
        m_fkptr->stop_work();
        return;
    }
    else
    {
        m_fkptr->m_dwfilenumber++;
        m_fkptr->m_yddlg->SetScanWindowPath(data);
        m_fkptr->m_yddlg->SetScanProcessCTL(m_fkptr->m_dwfilenumber * 100 / m_fkptr->m_allfienumber);
        m_fkptr->ScanFile(data);
    }

}

void PathQueueSet::set_fkptr(std::shared_ptr<ProcessFramework>& ptr)
{
    m_fkptr = ptr;
}

void DirQueueSet::print_byte_data(std::wstring& data, size_t length)
{
    if (data.length() != length && !m_fkptr)
    {
        return;
    }
    if (data == L"end") //目录解析完成，可以通知工作线程结束
    {
        m_fkptr->AddPath(L"end");
        return;
    }
    if (data.back() != L'\\') {
        // Add a backslash to the end of the path
        data += L'\\';
    }
    std::list<std::wstring> filePaths;
    traverseDirectory(data, filePaths);
    for (auto it = filePaths.begin(); it != filePaths.end(); it++)
    {
        m_fkptr->AddPath(it->c_str());
    }
}

void DirQueueSet::set_fkptr(std::shared_ptr<ProcessFramework>& ptr)
{
    m_fkptr = ptr;
}

void DirQueueSet::traverseDirectory(const std::wstring& directory, std::list<std::wstring>& filePaths)
{
    std::wstring searchPath = directory + L"*.*";
    WIN32_FIND_DATAW fd;
    HANDLE hFind = ::FindFirstFileW(searchPath.c_str(), &fd);

    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if (wcscmp(fd.cFileName, L".") != 0 && wcscmp(fd.cFileName, L"..") != 0)
                {
                    std::wstring subDirectory = directory + fd.cFileName;
                    if (subDirectory.back() != L'\\') {
                        // Add a backslash to the end of the path
                        subDirectory += L'\\';
                    }
                    traverseDirectory(subDirectory, filePaths);
                }
            }
            else
            {
                std::wstring filePath = directory + fd.cFileName;
                filePaths.push_back(filePath);
            }
        } while (::FindNextFileW(hFind, &fd));

        ::FindClose(hFind);
    }
}